const workerpool = require('workerpool');
const fs = require('fs')
const path = require('path')
const { folder2} = require('./config.js');
const folderPath = path.join(__dirname, ...folder2.split('/'));
(async () => {
    fs.mkdirSync(`${folderPath}/../汇总`, { recursive: true })
    let files = fs.readdirSync(folderPath);
    const result = []
    for (let i = 0; i < files.length; i += 100) {
        result.push(files.slice(i, i + 100));
    }
    let completedTasks = 0;// 记录完成的任务数量,用于输出进度信息
    const task_count = result.length;
    const pool = workerpool.pool(
        'moreWorker.js',
        {
            workerType: 'process', // process创建独立进程,thread创建独立线程
            maxWorkers: 12, // 最大线程数
        });

    try {
        const promises = [];
        for (let taskId = 1; taskId <= task_count; taskId++) {
            const promise = pool.exec('worker', [{ data: result[taskId - 1],taskId}])
                .then(res => {
                    completedTasks++
                    console.log(`文件读取中...${completedTasks}/${task_count}`);
                })
                .catch(err => {
                    console.error(`[线程_${taskId}]:`, err.message);
                });
            promises.push(promise);
        }

        await Promise.all(promises);
        console.log(`文件汇合完毕！`);

        await pool.terminate();
    } catch (err) {
        console.error('任务执行出错:', err);
        await pool.terminate();
    }
})();